#include "log_mgr.h"
#include <assert.h>

extern std::string g_sLogPath;

CLogMgr::CLogMgr()
: m_bInit(false)
{
	;
}

CLogMgr::~CLogMgr()
{
	std::map<LOG_TYPE, CLogger*>::iterator it = m_mapLogger.begin();
	for( ; it != m_mapLogger.end(); it++)
	{
		delete it->second;
	}
}

bool CLogMgr::Init()
{
	if(m_bInit) return m_bInit;

	CLogger* pLogger = NULL;
	char sPath[512] = {0};	
	
	//debug
	pLogger = new CLogger;
	assert(pLogger);
	snprintf(sPath, sizeof(sPath), "%s/debug/connect", g_sLogPath.c_str());
	pLogger->Initialize(sPath);
	m_mapLogger[CONN] = pLogger; 

	pLogger = new CLogger;
	assert(pLogger);
	snprintf(sPath, sizeof(sPath), "%s/debug/package", g_sLogPath.c_str());
	pLogger->Initialize(sPath);
	m_mapLogger[PKG] = pLogger; 

	pLogger = new CLogger;
	assert(pLogger);
	snprintf(sPath, sizeof(sPath), "%s/debug/package_hex", g_sLogPath.c_str());
	pLogger->Initialize(sPath);
	m_mapLogger[PKG_HEX] = pLogger; 

	//error
	pLogger = new CLogger;
	assert(pLogger);
	snprintf(sPath, sizeof(sPath), "%s/error", g_sLogPath.c_str());
	pLogger->Initialize(sPath);
	m_mapLogger[ERROR] = pLogger; 

	//state
	pLogger = new CLogger;
	assert(pLogger);
	snprintf(sPath, sizeof(sPath), "%s/stat", g_sLogPath.c_str());
	pLogger->Initialize(sPath);
	m_mapLogger[STAT] = pLogger;

	m_bInit = true;

	return m_bInit;
}

CLogger& CLogMgr::GetLogger(LOG_TYPE enType )
{
	assert(m_bInit);
	
	std::map<LOG_TYPE, CLogger*>::iterator it = m_mapLogger.find(enType);
	if(it == m_mapLogger.end()) assert(0);
 
	return *(it->second) ;
}

